/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.layers;

import java.util.Observable;
import java.util.Observer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import id.duapuluhdua.dakidaki.activities.ActivityMain;
import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.map.Screen;
import id.duapuluhdua.dakidaki.map.Viewport;

/**
 *
 */
public class LayerLocation extends Layer implements Observer
{
  /** Constants */
  private final static int RADIUS = 6;
  private final static int BORDER_RADIUS = 10;

  /** Fields */
  private static LayerLocation self;
  private Paint mPaintLocation;
  private Paint mPaintLocationBorder;
  private RectF rtPoint;

  /**
   * The constructor.
   *
   * @param parent
   * @param screen
   */
  public LayerLocation(CommonActivity parent, Screen screen)
  {
    super(parent, screen);

    self = this;

    // Paint location
    mPaintLocation = new Paint();
    mPaintLocation.setAntiAlias(true);
    mPaintLocation.setColor(0xff6090a0);
    mPaintLocation.setStrokeWidth(1);
    mPaintLocation.setStyle(Paint.Style.FILL_AND_STROKE);

    mPaintLocationBorder = new Paint();
    mPaintLocationBorder.setAntiAlias(true);
    mPaintLocationBorder.setColor(0xff104050);
    mPaintLocationBorder.setStrokeWidth(1);
    mPaintLocationBorder.setStyle(Paint.Style.STROKE);

    rtPoint = new RectF();

    // Observe location status
    super.startObserveLocation(this);
  }

  @Override
  public void update(Observable observable, Object data)
  {
    super.updateLocation(observable, data);
  }

  /**
   * If the viewport is in LOCATION_MODE then zoom to the new identified
   * location.
   */
  @Override
  public void updateLocation()
  {
    final int currentViewportMode = getScreen().getCurrentViewportMode();
    if (currentViewportMode == Viewport.MODE_LOCATION) {
      if (ActivityMain.loader.isLocationInUse()) {
        resetViewport();
      }
    }
  }

  @Override
  public void initData()
  {
    // Do nothing.
  }

  @Override
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    // Do nothing.
  }

  public static void resetViewport()
  {
    synchronized (self.mLocationGeoPoint)
    {
      if (self.isLocationEnabled()) {
        self.mScreen.resetViewport(self.mLocationGeoPoint, Viewport.MODE_LOCATION);
      }
    }
  }

  @Override
  public void doDraw(Canvas canvas, Rect area)
  {
    if (super.isLocationEnabled()) {
      updateLocationScreenPoint();

      final boolean bSecured = ActivityMain.loader.isLocationInUse();

      // Draw location point
      rtPoint.set(mLocationScreenPoint.x - RADIUS,
          mLocationScreenPoint.y - RADIUS,
          mLocationScreenPoint.x + RADIUS,
          mLocationScreenPoint.y + RADIUS);

      mPaintLocation.setColor(0xff205060);
      canvas.drawOval(rtPoint, mPaintLocation);

      rtPoint.inset(3, 3);

      mPaintLocation.setColor(0xff80b0c0);
      canvas.drawOval(rtPoint, mPaintLocation);

      // Show selection
      mPaintLocationBorder.setStrokeWidth(bSecured ? 2 : 1);

      final int iNewBorderRadius = bSecured ? BORDER_RADIUS + 1 : BORDER_RADIUS;

      // Draw border
      rtPoint.set(mLocationScreenPoint.x - iNewBorderRadius,
          mLocationScreenPoint.y - iNewBorderRadius,
          mLocationScreenPoint.x + iNewBorderRadius,
          mLocationScreenPoint.y + iNewBorderRadius);

      canvas.drawOval(rtPoint, mPaintLocationBorder);
    }
  }

  @Override
  public void updateObjectsState()
  {
    // Do nothing.
  }
}
